package cn.wolfcode.service.impl;

import cn.wolfcode.common.web.Result;
import cn.wolfcode.domain.Product;
import cn.wolfcode.domain.SeckillProduct;
import cn.wolfcode.domain.SeckillProductVo;
import cn.wolfcode.feign.ProductFeignApi;
import cn.wolfcode.mapper.SeckillProductMapper;
import cn.wolfcode.redis.SeckillRedisKey;
import cn.wolfcode.service.ISeckillProductService;
import com.alibaba.fastjson.JSON;
import org.apache.commons.lang.StringUtils;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Service
public class SeckillProductServiceImpl implements ISeckillProductService {
    @Autowired
    private SeckillProductMapper seckillProductMapper;
    @Autowired
    private StringRedisTemplate redisTemplate;
    @Autowired
    private RocketMQTemplate rocketMQTemplate;
    @Autowired
    private ProductFeignApi productFeignApi;

    @Override
    public List<SeckillProductVo> selectByTime(int time) {
        //先查询出t_seckill_product表中的数据
        List<SeckillProduct> seckillProducts = seckillProductMapper.queryCurrentlySeckillProduct(time);
        List<Long> ids = new ArrayList<>();
        for (SeckillProduct seckillProduct : seckillProducts) {
            //获取出productId，放入到数组中
            ids.add(seckillProduct.getProductId());
        }
        //远程调用商品服务去查询商品数据
        List<Product> products = productFeignApi.findByIds(ids).getData();
        //聚合操作
        List<SeckillProductVo> vos = new ArrayList<>();
        Map<Long,Product> pMap = new HashMap<>();
        for (Product product : products) {
            pMap.put(product.getId(),product);
        }
        for (SeckillProduct seckillProduct : seckillProducts) {
            SeckillProductVo vo = new SeckillProductVo();
            Product product = pMap.get(seckillProduct.getProductId());
            BeanUtils.copyProperties(product,vo);
            BeanUtils.copyProperties(seckillProduct,vo);
            vos.add(vo);
        }
        return vos;
    }

    @Override
    public List<SeckillProductVo> queryByTime(int time) {
        List<Object> values = redisTemplate.opsForHash().values(SeckillRedisKey.SECKILL_PRODUCT_LIST.getRealKey(time + ""));
        List<SeckillProductVo> vos = new ArrayList<>();
        for (Object value : values) {
            vos.add(JSON.parseObject(value+"",SeckillProductVo.class));
        }
        return vos;
    }

    @Override
    public SeckillProductVo queryByTimeAndSid(int time, Long seckillId) {
        Object o = redisTemplate.opsForHash().
                get(SeckillRedisKey.SECKILL_PRODUCT_LIST.getRealKey(time + ""), seckillId + "");
        if (StringUtils.isEmpty(o+"")){
            return null;
        }
        return JSON.parseObject(o + "",SeckillProductVo.class);
    }

    @Override
    public void syncRedisStockCount(Integer time, Long seckillId) {
        //把真实库存拿出来重新放入Redis当中
        int stockCount = seckillProductMapper.getStockCount(seckillId);
        redisTemplate.opsForHash().
                put(SeckillRedisKey.SECKILL_STOCK_COUNT_HASH.getRealKey(time + ""),
                        seckillId + "",stockCount + "");
    }
}
